package com.lonely.fx.property;

import javafx.beans.property.SimpleMapProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;

import java.util.HashMap;

/**
 * @date: 2022-07-22 17:16
 * @author: ztkj-hzb
 * @description: 针对 Map 类型的属性设置
 **/
public class MapPropertySettingMain {


    public static void main(String[] args) {


        ObservableMap<String,String> observableMap = FXCollections.observableHashMap();
        observableMap.put("name","aaa");
        observableMap.put("age","111");

        SimpleMapProperty<String, String> mapProperty = new SimpleMapProperty<>(observableMap);

        //map变化监听类
        mapProperty.addListener(new ChangeListener<ObservableMap<String, String>>() {
            @Override
            public void changed(ObservableValue<? extends ObservableMap<String, String>> observable, ObservableMap<String, String> oldValue, ObservableMap<String, String> newValue) {
                System.out.println(observable.hashCode());
                System.out.println(oldValue.hashCode());
                System.out.println(newValue.hashCode());
            }
        });


        mapProperty.addListener(new MapChangeListener<String, String>() {
            @Override
            public void onChanged(Change<? extends String, ? extends String> change) {
                //获取当前map
                ObservableMap<? extends String, ? extends String> changeMap = change.getMap();

                System.out.println("是否是添加元素"+change.wasAdded());
                System.out.println("是否是移除元素"+change.wasRemoved());

                //当前操作的key
                System.out.println("当前操作的key："+ change.getKey());
                System.out.println("当前新增的value："+ change.getValueAdded());
                System.out.println("当前删除的value："+ change.getValueRemoved());

            }
        });

        mapProperty.put("address","湖北省");
        mapProperty.put("name","独孤");

        HashMap<String, String> addMap = new HashMap<>();
        addMap.put("age","1222");
        addMap.put("sex","男");
        mapProperty.putAll(addMap);

    }


}
